'use strict';

module.exports = app => {
    const {
        INTEGER,
        STRING,
        TEXT,
        DATE,
    } = app.Sequelize;

    const Blog = app.model.define('blog', {
        id: {
            type: INTEGER,
            primaryKey: true,
            autoIncrement: true,
        },
        title: {
            type: STRING,
            validate: {
                min: 2,
                max: 50,
            },
        },
        summary: {
            type: STRING,
            validate: {
                min: 2,
                max: 255,
            },
        },
        content: {
            type: TEXT,
        },
        readSize: {
            type: INTEGER,
            defaultValue: 0,
        },
        commentSize: {
            type: INTEGER,
            defaultValue: 0,
        },
        tags: {
            type: STRING(100),
        },
        created_at: DATE,
        updated_at: DATE,

    });
    //belongsTo 一对一
    //Student.belongsTo(app.model.StudentInfo, { foreignKey: 'stuId', targetKey: 'id', as : 'studentInfo' })
    //   1、belongsTo 中 Student为源模型，StudentInfo为关联表模型（即目标模型）
    //   2、外键在Student中
    //   3、foreignKey 指定为源模型中的属性，targetKey为目标模型属性4、as 指定目标模型命名 ，例如结果中的studentInfo
    Blog.associate = function () {
        app.model.Blog.belongsTo(app.model.User);
        // app.model.Blog.belongsTo(app.model.Catalog);
        // app.model.Blog.hasMany(app.model.Comment, {
        //   as: 'comment',
        // });
    };
    return Blog;
};